/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * ProductList Class:
 * This class is separate from the Product class, but enables the Products and List of Products available to be modified
 * @author Vale
 *
 */
public class ProductList {

    private ArrayList<Product> prodList = new ArrayList<Product>();
    private final String PROD_AVAIL = "The products currently available, are:";
    private final String PRINT_CHOICE_INFO = "You can view specific Product Information by entering the corresponding number in the list above, or return to the main menu (Option: 0)";
    private final String INVALID_OPTION = "You did not enter a valid option.";
    private final String ERR = "Error: ";
    private final String REENTER_CHOICE = "Please enter your choice again.";
    private final String OPT = "Option: ";
    private final String PROD_NAME = "Product Name: ";
    private final String PROD_COST = "Product Cost: ";
    private final String PROD_SCHED = "Products Scheduled: ";
    private final String COMBO_SIZE = "Combo Size: ";
    private final String COMBO_CONSISTS = "Combo Consists of: ";
    private final String INV_REQD = "Inventory Required: ";
    private final String COMBO = "Combo";
    private final String CHOOSE = "You Can:";
    private final String RET_MAIN = "1. Return to Main Menu";
    private final String VIEW_PROD = "2. View more Products";
    private final String SCHED = "'s Scheduled: ";
    private final String NUM_SCHED = "Please enter the number of scheduled items for each product.";
    private final int ZERO = 0;
    private final int FALSE = -1;

    /**
     * void add(Product prod) Method:
     * Adds a Product (prod) to the Product List
     *
     */
    public void add(Product prod) {
        prodList.add(prod);
    }

    /**
     * void void remove(int index) Method:
     * Removes Product at index, from the Product List
     *
     */
    public void remove(int index) {
        prodList.remove(index);
    }

    /**
     * void remove(Product prod) Method:
     * Removes given Product (prod) from the Product List
     *
     */
    public void remove(Product prod) {
        prodList.remove(prod);
    }

    /**
     * boolean isEmpty() Method:
     * Checks if Product List is empty
     * @return True if empty, false otherwise
     *
     */
    public boolean isEmpty() {
        return prodList.isEmpty();
    }

    /**
     * int indexOf(Product prod) Method:
     * @param prod The product whose index is sought
     * @return Returns index of given Product
     *
     */
    public int indexOf(Product prod) {
        return prodList.indexOf(prod);
    }

    /**
     * int size() Method:
     * @return Returns Size of Product List
     *
     */
    public int size() {
        return prodList.size();
    }

    /**
     * Product get(int index) Method:
     * @param index The index of the Product
     * @return Returns Product from Product List at given index
     *
     */
    public Product get(int index) {
        return prodList.get(index);
    }

    /**
     * ArrayList<Product> getProdList() Method:
     * @return Returns List of Products
     *
     */
    public ArrayList<Product> getProdList() {
        return prodList;
    }

    /**
     * void printProducts() Method:
     * Prints all Products from Product List to Screen. Products can then be edited depending on user input, in which case other functions are called.
     * @return Returns when User is finished viewing Products
     *
     */
    public void printProducts() {
        String choice = null;
        System.out.println(PROD_AVAIL);
        for (int i = 0; i < prodList.size(); i++) {
            System.out.print('\t' + String.valueOf(i + 1) + ". ");
            System.out.println(prodList.get(i).getProdName());
        }
        System.out.println(PRINT_CHOICE_INFO);
        System.out.print(OPT);
        try {
            BufferedReader choiceIn = new BufferedReader(new InputStreamReader(System.in));
            choice = choiceIn.readLine();
            int checkChoice = checkInput(choice);
            if ((checkChoice >= ZERO) && (checkChoice <= prodList.size())) {
                if (checkChoice == 0) {
                    return;
                } else if (checkChoice == FALSE) {
                    System.out.println(INVALID_OPTION);
                    printProducts();
                } else {
                    printProdInfo(checkChoice - 1);
                    return;
                }
            } else {
                System.out.println(INVALID_OPTION);
                printProducts();
            }
        } catch (IOException e) {
            System.out.println(ERR + e);
            System.out.println(REENTER_CHOICE);
            printProducts();
        }
    }

    /**
     * int checkInput(String choice) Method:
     * Takes the input choice from the main menu and checks to make sure it is correct.
     * @param choice This is the input String as entered by the user
     * @return Null String, invalid numbers or numbers out of bounds of array return FALSE, choiceIn otherwise
     *
     */
    public int checkInput(String choice) {
        try {
            int choiceIn = Integer.parseInt(choice);
            if (choice.equals("")) {
                return FALSE;
            } else {
                return choiceIn;
            }
        } catch (NumberFormatException x) {
            return FALSE;
        }
    }

    /**
     * void printProdInfo(int prod) Method:
     * Prints the product information for the chosen Product, at the index specified by prod.
     * @param prod  Index of product to get information for
     * @return Function returns to Main Menu/Product Menu pending on user input
     *
     */
    public void printProdInfo(int prod) {
        String choice = null;
        if (prodList.get(prod).getProdName().contains(COMBO)) {
            Combo comboItem = (Combo) prodList.get(prod);
            System.out.println();
            System.out.println(PROD_NAME + comboItem.getProdName());
            System.out.println(PROD_COST + comboItem.getProdCost());
            System.out.println(PROD_SCHED + comboItem.getProdSched());
            System.out.println(COMBO_SIZE + comboItem.convertComboSize(comboItem.getComboSize()));
            System.out.println(COMBO_CONSISTS + comboItem.convertComboSize(comboItem.getComboSize()) + " " + comboItem.getDrinkName() + ", " + comboItem.convertComboSize(comboItem.getComboSize()) + " Chips, " + comboItem.getMainProd().getProdName());
            System.out.println(INV_REQD);
            for (int i = 0; i < prodList.get(prod).getInventList().size(); i++) {
                System.out.print('\t');
                System.out.println(prodList.get(prod).getInventList().get(i).getInvAmount() + " " + prodList.get(prod).getInventList().get(i).getInvName());
            }
        } else {
            System.out.println();
            System.out.println(PROD_NAME + prodList.get(prod).getProdName());
            System.out.println(PROD_COST + prodList.get(prod).getProdCost());
            System.out.println(PROD_SCHED + prodList.get(prod).getProdSched());
            System.out.println(INV_REQD);
            for (int i = 0; i < prodList.get(prod).getInventList().size(); i++) {
                System.out.print('\t');
                System.out.println(prodList.get(prod).getInventList().get(i).getInvAmount() + " " + prodList.get(prod).getInventList().get(i).getInvName());
            }
        }
        System.out.println();
        System.out.println(CHOOSE);
        System.out.println('\t' + RET_MAIN);
        System.out.println('\t' + VIEW_PROD);
        System.out.print(OPT);
        try {
            BufferedReader choiceIn = new BufferedReader(new InputStreamReader(System.in));
            choice = choiceIn.readLine();
            switch (checkInput(choice)) {
                case 1:
                    return;
                case 2:
                    printProducts();
                    break;
                default:
                    System.out.println(INVALID_OPTION);
                    printProdInfo(prod);
            }
        } catch (IOException x) {
            System.out.println(ERR + x);
        }

    }

    /**
     * void scheduleProducts(InventoryList invList, ProductList prodList) Method:
     * Prompts the user to enter the number of products scheduled for each Product. YET TO BE IMPLEMENTED: invList and prodList are used to allow required inventory to be calculated
     * @param invList Inventory List for program
     * @param prodList Product List for Program
     * @return Function returns to Main Menu/Product Menu pending on user input
     *
     */
    public void scheduleProducts(InventoryList invList, ProductList prodList) {
        String choice = null;
        System.out.println(NUM_SCHED);
        for (int i = 0; i < prodList.size(); i++) {
            System.out.print(prodList.get(i).getProdName() + SCHED);
            try {
                BufferedReader choiceIn = new BufferedReader(new InputStreamReader(System.in));
                choice = choiceIn.readLine();
                int checkChoice = checkInput(choice);
                if ((checkChoice < ZERO) || (checkChoice == FALSE)) {
                    System.out.println(INVALID_OPTION);
                    i--;
                } else {
                    prodList.get(i).setProdSched(checkChoice);
                }
            } catch (IOException e) {
                System.out.println(ERR + e);
                System.out.println(REENTER_CHOICE);
                printProducts();
            }
        }
    }
}
